{% extends 'admins/base.html' %}
{% block link %}
    <link rel="stylesheet" href="/static/plugins/datatables/dataTables.bootstrap.css">
{% endblock %}
{% block body %}
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                博文列表
                <small>包含已发布和未发布的博文</small>
            </h1>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                        <!-- /.box-header -->
                        <div class="box-body">
                            <table id="articleList" class="table table-bordered table-striped">
                                <thead>
                                <tr>
                                    <th>文章标题</th>
                                    <th>创建时间</th>
                                    <th>发布时间</th>
                                    <th>阅读数</th>
                                    <th>评论数</th>
                                    <th>状态</th>
                                    <th>类别</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% for article in articles %}
                                    <tr id="Article{{ article.id }}">
                                        <td>{{ article.title }}</td>
                                        <td>{{ article.created_time }}</td>
                                        <td>{{ article.release_time }}</td>
                                        <td>{{ article.read }}</td>
                                        <td>{{ article.get_comment_num }}</td>
                                        <td>
                                            {% if article.status == '0' %}
                                                <span class="label label-info">发布</span>
                                            {% elif article.status == '1' %}
                                                <span class="label label-danger">存稿</span>
                                            {% endif %}
                                        </td>
                                    <td>
                                            {% if article.category == '0' %}
                                                <span class="label label-info">原创</span>
                                            {% elif article.category == '1' %}
                                                <span class="label label-danger">转载</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <button onclick="EditArticle({{ article.id }})" type="button" class="btn btn-warning" style="font-size: 12px">编辑</button>
                                            <button onclick="DeleteArticle({{ article.id }})" type="button" class="btn btn-danger" style="font-size: 12px">删除</button>
                                        </td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        </div>
                        <!-- /.box-body -->
                    </div>
                    <!-- /.box -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
{% endblock %}

{% block javascript %}
    <script type="text/javascript">
			$(document).ready(function(){
				$('#articleul').addClass("menu-open");
				$('#articlelist').addClass("active");
				$('#articleli').addClass("active");
		});
    </script>
     <!-- DataTables -->
    <script src="/static/dist/js/jquery.cookie.js"></script>
    <script src="/static/plugins/datatables/jquery.dataTables.min.js"></script>
    <script src="/static/plugins/datatables/dataTables.bootstrap.min.js"></script>
    <!-- page script -->
    <script>
		var csrftoken = $.cookie('csrftoken');
	function csrfSafeMethod(method) {
		// these HTTP methods do not require CSRF protection
		return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
	}
	$.ajaxSetup({
		beforeSend: function(xhr, settings) {
			if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
				xhr.setRequestHeader("X-CSRFToken", csrftoken);
			}
		}
	});
		function DeleteArticle(id) {
            if(confirm("确认要删除此文章，删除后不可恢复哦？")){
                    $.ajax({
                        url:'{% url 'delarticle' %}',
                        type:'POST',
                        data:{modify:id},
                        success:function (arg) {
                            window.location.reload();
                        },
                        error:function () {
                            console.log('failed');
                        }
                    });
                }
	};
        </script>
    <script>
        $(function () {
            $("#articleList").DataTable();
        });
        function EditArticle(ID) {
            location.href = "{% url 'editarticle' %}" + '?aid=' + ID
        }
    </script>
{% endblock %}
